package com.jinlejia.bbs.logic;

import android.app.Activity;
import android.content.Context;

import com.alibaba.fastjson.JSONObject;
import com.jinlejia.bbs.http.utils.BBSConfig;
import com.jinlejia.bbs.http.utils.HttpCallBack;

import static com.jinlejia.bbs.http.utils.HttpUtils.JsonParse;
import static com.jinlejia.bbs.http.utils.HttpUtils.httpCallBack;
import static com.jinlejia.bbs.http.utils.HttpUtils.httpCallForm;

/**
 * @author 王进 on 2019/3/25.
 */

public class AskLogic {
    private Context context;

    public AskLogic(Activity activity,LogicCallBack callBack){
        context =activity;
        this.callBack = callBack;
    }
    public void execute(String text){
        send(text);
    }
    private LogicCallBack callBack;
    public interface LogicCallBack{
        void onResult(boolean flag);
    }

    private void send(String text){
        JSONObject json = new JSONObject();
        json.put("topicTitle",text);
        httpCallForm(BBSConfig.saveTopic,json)
                .execute(httpCallBack(context, new HttpCallBack() {
                    @Override
                    public void onError(Exception e) {
                        e.printStackTrace();
                        callBack.onResult(false);
                    }

                    @Override
                    public void onResponse(JsonParse parse) {
                        boolean flag = parse.parse(Boolean.class);
                        callBack.onResult(flag);
                    }

                    @Override
                    public void onNoResponse(int code) {
                        callBack.onResult(false);
                    }
                }));
    }
}
